﻿using ZYCommon;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using TextEditor.TextDocument.Document;
using System.Drawing.Drawing2D;
using System.Text;

namespace TextEditor
{


    public class A_SaveImage : A_Action
    {
        public override string ActionName()
        {
            return "saveimage";
        }
        public override bool Execute()
        {
            Bitmap preViewImage = base.myOwnerDocument.GetPreViewImage();
            if (preViewImage != null)
            {
                preViewImage.Save(base.Param1, ImageFormat.Png);
                preViewImage.Dispose();
                return true;
            }
            return false;
        }

        public override bool UIExecute()
        {
            //using (SaveFileDialog dialog = new SaveFileDialog())
            //{
            //    dialog.FileName = base.myOwnerDocument.Info.FileName;
            //    dialog.CheckFileExists = false;
            //    dialog.CheckPathExists = true;
            //    dialog.OverwritePrompt = true;
            //    dialog.Filter = "jpg图片(.jpg)|*.jpg";// "所有文件|*.*|JPG图片(.jpg)|*.jpg|GIF图片(.gif)|*.gif|png图片(.png)|*.png";
            //    if (dialog.ShowDialog() == DialogResult.OK)
            //    {
            //        this.OwnerDocument.Info.Printing = true;
            //        this.OwnerDocument.RefreshSize();
            //        this.OwnerDocument.RefreshLine();
            //        this.OwnerDocument.EnableHide = true;
            //        this.OwnerDocument.RefreshElements();
            //        this.OwnerDocument.RefreshSize();
            //        this.OwnerDocument.RefreshLine();
            //        this.OwnerDocument.EnableHide = false;
            //        bool showParagraphFlag = base.OwnerDocument.Info.ShowParagraphFlag;
            //        base.OwnerDocument.Info.ShowParagraphFlag = false;
            //        base.OwnerDocument.Info.FieldUnderLine = false;
            //        base.OwnerDocument.Info.ShowExpendHandle = false;
            //        if (base.myOwnerDocument.OwnerControl.PageViewMode)
            //        {
            //            string fileName = dialog.FileName;
            //            fileName = dialog.FileName;
            //            if (fileName.IndexOf(".") > 0)
            //            {
            //                fileName = fileName.Substring(0, fileName.IndexOf("."));
            //            }
            //            for (int i = 0; i < base.myOwnerDocument.Pages.Count; i++)
            //            {
            //                using (Image bitmap = (Image)base.myOwnerDocument.GetPreViewImage(i + 1, 0, 0))
            //                {                                
            //                    if (bitmap != null)
            //                    {
            //                        //string[] file = Directory.GetFiles(@"\\192.168.36.120\UpdateFile");
            //                        //bitmap.Save(@"\\192.168.36.120\UpdateFile\" + file.Length.ToString() + "_" + i.ToString() + ".jpg", ImageFormat.Jpeg);
            //                       bitmap.Save(fileName + "_" + i.ToString() + ".jpg", ImageFormat.Jpeg);
            //                    }
            //                }
            //            }
            //        }
            //        else
            //        {
            //            using (Bitmap bitmap2 = base.myOwnerDocument.GetPreViewImage())
            //            {
            //                if (bitmap2 != null)
            //                {
            //                    bitmap2.Save(dialog.FileName, ImageFormat.Png);
            //                    bitmap2.Dispose();
            //                }
            //            }
            //        }
            //        this.OwnerDocument.Info.ShowExpendHandle = true;
            //        this.OwnerDocument.Info.ShowParagraphFlag = true;
            //        this.OwnerDocument.Info.FieldUnderLine = true;
            //        this.OwnerDocument.Info.Printing = false;
            //        this.OwnerDocument.RefreshSize();
            //        this.OwnerDocument.RefreshLine();
            //        return true;
            //    }
            //}

            UcEditorControl ed = new UcEditorControl(); //new个编辑器对象 可以设为全局变量
            ed.EMRDoc.Locked = true;  //设置属性
            ed.EMRDoc.PageHeader = null; //清空表头
            //ed.EMRDoc.InpatientInfo = this.myOwnerDocument.Us.InpatientInfo; //设置病人信息
            // ed.EMRDoc.Us.Tid = this.OwnerDocument.Us.Tid
            ed.EMRDoc.Us = this.OwnerDocument.Us;


            XmlDocument document = new XmlDocument(); //xml文档对象
            document.PreserveWhitespace = true;       //不忽略空白部分
            document.LoadXml("<emrtextdoc/>");        //这里加载文件
            this.OwnerDocument.ToXML(document.DocumentElement);

            //XmlDocument newDocument = new XmlDocument();
            //newDocument.PreserveWhitespace = true;
            //newDocument.LoadXml("<emrtextdoc/>");
            //foreach (XmlNode item in document.GetElementsByTagName("body"))
            //{
            //    XmlNode bodyNode = newDocument.CreateElement("body");
            //    foreach (XmlNode item2 in item)
            //    {
            //        XmlNode node2 = newDocument.CreateElement(item2.Name);
            //        foreach (XmlAttribute attribute in item2.Attributes)
            //        {
            //            ((XmlElement)node2).SetAttribute(attribute.Name, attribute.Value);
            //        }
            //        node2.InnerXml = item2.InnerXml;
            //        bodyNode.AppendChild(node2);
            //        if (item2.Name == "div" && item2.Attributes["title"] != null && item2.Attributes["title"].Value.Contains("家族史"))
            //        {
            //            break;
            //        } 
            //    }
            //    XmlNode newNode = newDocument.CreateElement("table");
            //    ((XmlElement)newNode).SetAttribute("tableLock", "1");
            //    newNode.InnerXml = "<row id=\"C1B1C10640\" width=\"679\" min-height=\"40\"><cell id=\"C1B1C10641\" width=\"334\" candelete=\"1\" isVisble=\"0\"><p operatercreater=\"0\" /></cell><cell id=\"C1B1C10642\" width=\"334\" candelete=\"1\" isVisble=\"0\"><p operatercreater=\"0\" /></cell></row><row id=\"C1B1C10643\" width=\"679\"><cell id=\"C1B1C10644\" width=\"334\" candelete=\"1\" isVisble=\"0\"><span operatercreater=\"0\">    病史签名:</span><p operatercreater=\"0\" /></cell><cell id=\"C1B1C10645\" width=\"334\" candelete=\"1\" isVisble=\"0\"><span operatercreater=\"0\">某某签名:</span><p operatercreater=\"0\" /></cell></row>";
            //    bodyNode.AppendChild(newNode);
            //    newDocument.DocumentElement.AppendChild(bodyNode);
            //}
        
            //newDocument.Save("D://123123.xml");
            ed.EMRDoc.FromXML(document.DocumentElement, true);
            string[] strs; //存储文书对应页码 格式 "文书ID|页码"
            Image[] imgs = ed.EMRDoc.GetDocForImages(out strs); //获得文书生成的图片集合
            for (int i = 0; i < imgs.Length; i++)
            {
                Image img = imgs[i];
                UpLoadImg.GetPicThumbnail(img, "D://" + (i + 1).ToString() + ".jpg", 100);
            }
            return true;
        }
    }
}

